﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using UtilityExtensions;
using System.Xml.Linq;
using System.Configuration;

namespace DAL
{
    public static partial class DbUtil
    {
        private const string DbKEY = "DBGestion";
        //private static CMSDataContext _idb;
        private static dbGestionDataContext InternalDb
        {
            get
            {

                return (dbGestionDataContext)HttpContext.Current.Items[DbKEY];
            }
            set
            {

                HttpContext.Current.Items[DbKEY] = value;
            }
        }

        public static String strConnString =
           ConfigurationManager.ConnectionStrings["dbGestionConnectionString"].ConnectionString;

        public static dbGestionDataContext Db
        {
            get
            {
                if (HttpContext.Current == null)
                    return new dbGestionDataContext(strConnString);
                if (InternalDb == null)
                {
                    InternalDb = new dbGestionDataContext(strConnString);
                    InternalDb.CommandTimeout = 1200;
                }
                return InternalDb;
            }
            set
            {
                InternalDb = value;
            }
        }
        public static void LogActivity(string activity)
        {
            LogActivity(activity, true);
        }
        public static void LogActivity(string activity, bool recordPageUrl)
        {
            //var db = new CMSDataContext(Util.ConnectionString);
            //int? uid = Util.UserId;
            //if (uid == 0)
            //    uid = null;
            //var a = new ActivityLog
            //{
            //    ActivityDate = Util.Now,
            //    UserId = uid,
            //    Activity = activity,
            //    PageUrl = recordPageUrl ? HttpContext.Current.Request.RawUrl : null,
            //};
            //db.ActivityLogs.InsertOnSubmit(a);
            //db.SubmitChanges();
            //db.Dispose();
        }
        public static void DbDispose()
        {
            if (InternalDb != null)
            {
                InternalDb.Dispose();
                InternalDb = null;
            }
        }
//        public static string TopNotice()
//        {
//            //var hc = HttpRuntime.Cache[Db.Host + "topnotice"] as string;
//            //if (hc == null)
//            //{
//            //    var h = Content("TopNotice");
//            //    if (h != null)
//            //        hc = h.Body;
//            //    else
//            //        hc = string.Empty;
//            //    HttpRuntime.Cache[Db.Host + "topnotice"] = hc;
//            //}
//            //return hc;
//        }
//        public static string HeaderImage(string def)
//        {
//            //var hc = HttpRuntime.Cache[Db.Host + "headerimg"] as string;
//            //if (hc == null)
//            //{
//            //    var h = Content("HeaderImg");
//            //    if (h != null)
//            //        hc = h.Body;
//            //    else
//            //        hc = def;
//            //    HttpRuntime.Cache[Db.Host + "headerimg"] = hc;
//            //}
//            //return hc;
//        }
//        public static string Header()
//        {
//            var hc = HttpRuntime.Cache[Db.Host + "header"] as string;
//            if (hc == null)
//            {
//                var h = Content("Header");
//                if (h != null)
//                    hc = h.Body;
//                else
//                    hc = @"
//<div id='CommonHeaderImage'>
//    <a href='/'><img src='/images/headerimage.jpg' /></a>
//</div>
//<div id='CommonHeaderTitleBlock'>
//    <h1 id='CommonHeaderTitle'>Bellevue Baptist Church</h1>
//    <h2 id='CommonHeaderSubTitle'>Feed My Sheep</h2>
//</div>
//";
//                HttpRuntime.Cache[Db.Host + "header"] = hc;
//            }
//            return hc;
//        }
//        //public static Content Content(string name)
//        //{
//        //    return DbUtil.Db.Contents.SingleOrDefault(c => c.Name == name);
//        //}
//        //public static string Content(string name, string def)
//        //{
//        //    var content = DbUtil.Db.Contents.SingleOrDefault(c => c.Name == name);
//        //    if (content != null)
//        //        return content.Body;
//        //    return def;
//        //}

//        //public static string SystemEmailAddress { get { return Db.Setting("SystemEmailAddress", ""); } }
//        //public static string AdminMail { get { return Db.Setting("AdminMail", SystemEmailAddress); } }
//        //public static string StartAddress { get { return Db.Setting("StartAddress", "2000+Appling+Rd,+Cordova,+Tennessee+38016"); } }
//        //public static bool CheckRemoteAccessRole { get { return Db.Setting("CheckRemoteAccessRole", "") == "true"; } }

        public const string MiscTagsString = "Misc Tags";
        public const int TagTypeId_Personal = 1;
        public const int TagTypeId_OrgMembersOnly = 3;
        public const int TagTypeId_CouplesHelper = 4;
        public const int TagTypeId_AddSelected = 5;
    }
}
